$(function () {
    var query_datagrid = $("#query_datagrid")
    var query_dialog = $("#query_dialog")
    var query_form = $("#query_form")
    var product_datagrid = $("#product_datagrid")
    query_dialog.dialog({
        closed: true,
        title: '缴费处理',
        width: 400,
        height: 600,
        buttons: '#dialog_buttons'
    })
    product_datagrid.datagrid({
        title:'已购产品列表',
        fitColumns:true,
        // fit:true,
        columns: [[
            {field: 'productName', title: '险种',width:100},
            {field: 'annualFee', title: '险种金额',width:100},
        ]
        ]
    })
    query_datagrid.datagrid({
        url: "/temporary/query.do",
        fitColumns: true, // 自适应列
        fit: true,
        striped: true,  // 斑马线
        pagination: true,
        pageSize: 10,
        pageNumber: 1,
        singleSelect: true, // 单选
        toolbar: "#query_toolbar",
        columns: [
            [
                {field: 'id', hidden: true},
                {
                    field: 'client.name', title: '投保人', width: 80, formatter: function (value, row) {
                        if (row.client) {
                            return row.client.name
                        }
                    }
                },
                {field: 'sn', title: '保单号', width: 80},
                {field: 'client.tel', title: '电话', width: 80,formatter: function (value, row) {
                        if (row.client) {
                            return row.client.tel
                        }
                    }},
                {field: 'client.address', title: '客户地址', width: 80,formatter: function (value, row) {
                        if (row.client) {
                            return row.client.address
                        }
                    }},
                {field: 'client.identitynum', title: '身份证号', width: 80,formatter: function (value, row) {
                        if (row.client) {
                            return row.client.identitynum
                        }
                    }},
                {field: 'totalAmount', title: '保费金额', width: 80},
                {
                    field: 'time', title: '保险年限/年', width: 80
                },
                {field: 'payMethod', title: '缴费方式', width: 80},
                {field: 'beginDate', title: '起保日期', width: 80},
                {field: 'endDate', title: '止保日期', width: 80},

                {
                    field: 'organization', title: '保险机构', width: 80, formatter: function (value, row) {
                        if (row.organization) {
                            return row.organization.name
                        }
                    }
                },
                {field: 'inputUser', title: '录入人', width: 80, formatter: function (value, row) {
                        if (row.inputUser) {
                            return row.inputUser.username
                        }
                    }},
            ]
        ]
    })
    var objMethod = {
        query: function () {
            //获取高级查询的参数值
            var keyword = $("#keyword").textbox('getValue')
            var sn = $("#sn").textbox('getValue')
            var beginDate = $("#beginDate").datebox('getValue')
            var endDate = $("#endDate").textbox('getValue')
            //重新加载数据表格
            query_datagrid.datagrid('load', {
                keyword: keyword,
                sn: sn,
                beginDate:beginDate,
                endDate:endDate
            });
        },
        refresh:function () {
            //刷新数据表格
            query_datagrid.datagrid('reload')
        },
        show: function () {
            var row = query_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", '请选择一条数据', 'warning')
                return
            }
            //回显数据
            row["client.name"] = row.client.name
            row["organization.name"] = row.organization.name
            row["car.carNum"] = row.car.carNum
            //回显数据表格
            $.get('/product/selectAllProduct.do', {insureId: row.id}, function (data) {
                product_datagrid.datagrid('loadData',data)
            })
            console.log(row);
            query_form.form('load', row)
            //打开对话框
            query_dialog.dialog('open')
        },
        cancel:function () {
            query_dialog.dialog('close')
        },
    }
    // 绑定事件
    $('a[data-cmd]').click(function () {
        objMethod[$(this).data('cmd')]();
    })
})